import Vue from 'vue'
import VueRouter from 'vue-router'
import homePage from '../views/homePage.vue'
import sginView from '../views/sginView.vue'
import download from '../views/downloadView.vue'
import allItems from '../views/allItems.vue'
import SearchPage from '../views/SearchPage.vue'
import userInfo from '../views/userInfo.vue'
import RankingList from '../views/RankingList.vue'
import shopPing from '../views/shopPing.vue'
import CollectionsView from '../views/CollectionsView.vue'
import CommodityView from '../views/CommodityView.vue'
import shoppingcarView from '../views/shoppingcarView.vue'

Vue.use(VueRouter)

const routes = [
  //主界面
  {
    path:'/',
    redirect: "/home"
  },
  {
    path:'/home',
    name:"home",
    component:homePage
  },
  //登录界面
  {
    path:'/sgin',
    name:"sgin",
    component:sginView,
  },
  
  //个人中心界面
  {
    path:'/userInfo',
    name:"userInfo",
    component:userInfo,
  },
  //藏品
  {
    path:'/CollectionsView',
    name:"CollectionsView",
    component:CollectionsView,
  },
  //下载界面
  {
    path:'/download',
    name:"download",
    component:download
  },
  //所有项目界面
  {
    path:'/allItems',
    name:"allItems",
    component:allItems
  },
  //点击搜索框后的页面
  {
    path:'/SearchPage',
    name:"SearchPage",
    component:SearchPage
  },
  //排行榜
  {
    path:'/RankingList',
    name:"RankingList",
    component:RankingList
  },
  //商城
  {
    path:'/shopPing',
    name:"shopPing",
    component:shopPing
  },
  //商城详情
  {
    path:'/CommodityView',
    name:"CommodityView",
    component:CommodityView
  },
  //购物车
  {
    path:'/shoppingcarView',
    name:"shoppingcarView",
    component:shoppingcarView
  },
  
]

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
return originalPush.call(this, location).catch(err => err)
}




const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 配置全局路由
router.beforeEach((to,from,next)=>{
  let token=window.localStorage.getItem("token");
  // 登录界面判断
  //判断这里面有没有token的这个值
  if(to.path == "/sgin"){
    next();
    return;
  }else{  //判断是否登录
    if(!token){
      next("/sgin");//进入登录界面
      return;
    }else{
      next();//执行下一个路由
    }

  }
})




export default router
